package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import leader.bean.MidTable;

import database.dao.MysqlCrud;

import teacher.bean.Midteaexp;
import teacher.bean.Project;
import teacher.bean.Teacher;
import tools.StringDate;

public class MidteaexpService {
	public List<MidTable> getAllExpById(Teacher teacher) {
		List<MidTable> lists = new ArrayList<MidTable>();
		String sql="select * from Midteaexp as acheve ,teacher as T where acheve.teaId = T.teaId and T.teaId=?";
		String[] parameter={String.valueOf(teacher.getTeaId())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql,parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			try {
				MidTable midtable = new MidTable();
				midtable.setMid(Integer.parseInt(map.get(0).toString()));
				midtable.setMtid(Integer.parseInt(map.get(1).toString()));
				midtable.setMeid(Integer.parseInt(map.get(2).toString()));
				StringDate date = new StringDate();				
				midtable.setMedate(date.StringToDate(date.DateToString(date.StringToDate(map.get(3).toString()))));
				lists.add(midtable);			
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return lists;
	}
	
	public boolean delMidTableByTeaId(Teacher teacher) {
		boolean flag = false;
		String sql="delete from midteaexp where teaid=?";
		String[]  parameter={String.valueOf(teacher.getTeaId())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	public boolean insertMidteaexp(int teacherid,int expertid,Date date) {
		boolean flag = false;
		String sql = "insert into Midteaexp(teaId,expId,date)"+" value(?,?,?)";
		String[] parameter = {
				String.valueOf(teacherid),
				String.valueOf(expertid),
				String.valueOf(date)
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	public int getCountOfMidTable(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from Midteaexp where teaId=?";
		String[] parameter={String.valueOf(teacher.getTeaId())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
}
